package com.example.parkdemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.webkit.JavascriptInterface
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.base.user.NewsParam
import com.example.base.user.NewsParamItem
import com.example.parkdemo.adapter.NewsAdapter
import com.example.parkdemo.databinding.ActivityMainBinding
import com.example.parkdemo.month.SelMonthActivity
import com.example.parkdemo.news.NewsActivity
import com.example.parkdemo.news.NewsViewModel
import com.example.parkdemo.parking.ParkingActivity
import com.example.parkdemo.repair.AddRepiairActivity
import com.example.parkdemo.repair.RepairActivity
import com.example.parkdemo.sign.SignActivity
import com.example.parkdemo.visitor.VisitorActivity
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, NewsViewModel>() {
    private lateinit var listener: MainWebViewJavaScriptFun

    // lateinit var newsAdapter: NewsAdapter
    override fun initData() {
        val home = SPUtils.getInstance().getString(Const.PARAM_HOME)
        binding.mainX5.loadUrl(home)
//        vm.NewsSuccess.observe(this){
//            Log.i("TagA","ss:$it")
//            val tt = Gson().fromJson(it,NewsParam::class.java)
//            newsAdapter.data.clear()
//            newsAdapter.data.addAll(tt)
//            newsAdapter.notifyDataSetChanged()
//        }
//        vm.videoSuccess.observe(this){
//
//            Log.i("TagA","ss:$it")
//        }
    }

    override fun initView() {
//        val map = mutableMapOf<String,Int>()
//        map[Const.PARAM_ID] = 0
//
//        val json = Gson().toJson(map)
//        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(),json)
//        vm.News(body)
//        newsAdapter = NewsAdapter()
//        binding.rv.adapter =newsAdapter
//        binding.rv.layoutManager = LinearLayoutManager(this)
//        vm.video(1,10)

        listener = MainWebViewJavaScriptFun()
        val home = SPUtils.getInstance().getString(Const.PARAM_HOME)
        binding.mainX5.loadUrl(home)
        //让当前网页内容可滚动
        binding.mainX5.overScrollMode = View.OVER_SCROLL_ALWAYS
        binding.mainX5.addJavascriptInterface(listener, "androidinfo")

        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
            ), 102
        )

    }

    inner class MainWebViewJavaScriptFun() {
        @JavascriptInterface
        fun androidsign(id: String) {
            //打开签到activity
            start(SignActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddculture(id: String) {
            //打开新闻activity
            Log.i("TagA", "添加新闻")
            start(NewsActivity::class.java)
        }

        @JavascriptInterface
        fun androidparking(id: String?) {
            //车位申请
            Log.i("TagA", "车位申请")
            start(ParkingActivity::class.java)
        }

        @JavascriptInterface
        fun androidapply(id: String?) {
            //我的申请
            Log.i("TagA", "我的申请")
        }

        @JavascriptInterface
        fun androidvisit(id: String?) {
            //来访预约
            Log.i("TagA", "来访预约")
            start(VisitorActivity::class.java)
        }

        @JavascriptInterface
        fun androidpatrol(id: String?) {
            //巡更管理
            Log.i("TagA", "巡更管理")
        }

        @JavascriptInterface
        fun androidnews(id: String?) {
            Log.i("TagA", "新闻")
        }

        @JavascriptInterface
        fun androidnoticelist(id: String?) {
            Log.i("TagA", "公告列表")
        }

        @JavascriptInterface
        fun androidrepair(id: String?) {
            //维修管理
            Log.i("TagA", "维修管理")
            start(RepairActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddrepair(id: String?) {
            //添加维修
            Log.i("TagA", "添加维修")
            start(AddRepiairActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddnotice(id: String?) {
            //公告管理
            Log.i("TagA", "公告管理")
        }

        @JavascriptInterface
        fun androidculture(id: String?) {
            //文化管理
            Log.i("TagA", "文化管理")
        }

        @JavascriptInterface
        fun androidnotice(id: String?) {
            //公告管理
            Log.i("TagA", "公告")
        }

        @JavascriptInterface
        fun androidattendance(id: String?) {
            //考勤管理
            Log.i("TagA", "考勤管理")
            start(SelMonthActivity::class.java)
        }

        @JavascriptInterface
        fun androidproperty(id: String?) {
            //物业审核
            Log.i("TagA", "物业审核")
        }

        @JavascriptInterface
        fun androidcheckculture(id: String?) {
            //文化审核
            Log.i("TagA", "文化审核")
        }

        @JavascriptInterface
        fun androidpeople(id: String?) {
            //人资管理
            Log.i("TagA", "人资管理")
        }
    }

    private fun start(clazz: Class<*>) {
        val intent = Intent(this, clazz)
        startActivity(intent)
    }
}